<?php

namespace App\Services\common;

use App\Helpers\Check;
use App\Models\common\SysCom;
use App\Services\BaseService;

/**
 * 组件实例 service
 *
 * @Description:
 *
 * @Author: zhx
 *
 * @Date: 2023-09-05
 *
 * @LastEditTime: 2023-09-05
 */
class SysComService extends BaseService
{
    /**
     * 数据查询
     *
     * @return array
     */
    public static function getData($requestData)
    {
        [$orderBy, $orderDir] = [
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'asc',
        ];
        $models = SysCom::query();
        if (isset($requestData['title'])) {
            $models->where('title', 'like', '%'.$requestData['title'].'%');
        }
        if (isset($requestData['parent_code'])) {
            $models->where('parent_code', $requestData['parent_code']);
        }
        if (isset($requestData['type'])) {
            $models->where('type', $requestData['type']);
        }
        $list = $models->orderBy($orderBy, $orderDir)->get();

        return $list;
    }

    /**
     * 编辑
     *
     * @return \Illuminate\Http\JsonResponse
     * User: zhx
     * Date: 2023/6/30 14:28
     */
    public static function edit($id, $requestData)
    {
        $models = SysCom::query();
        $models->where('id', $id);
        $result = $models->update($requestData);

        return $result;
    }

    /**
     * 添加
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface create
     */
    public static function create($requestData)
    {
        $result = SysCom::create($requestData);

        return $result;
    }

    /**
     * 删除
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function destroy($id)
    {
        $models = SysCom::query();
        $reult = $models->whereIn('id', $id)->delete();

        return $reult;
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function info($id)
    {
        $models = SysCom::query();

        return $models->where('id', $id)->first();
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function infoByCode($code)
    {
        $models = SysCom::query();

        return $models->where('code', $code)->first();
    }

    /**
     * 验证权限
     *
     * @return void
     */
    public static function menuPermissions(&$menu_list)
    {
        // 循环处理权限
        foreach ($menu_list as $menu_key => $menu_item) {
            // 跳过目录 和 无需授权的
            if ($menu_item['type'] == 'dir' || ! $menu_item['verify']) {
                continue;
            }
            unset($menu_list[$menu_key]);
        }

        // 判断目录下面是否有子菜单
        $parent_id = array_column($menu_list, 'parent_id');
        foreach ($menu_list as $menu_key => $menu_item) {
            // 跳过不是目录的
            if ($menu_item['type'] != 'dir') {
                continue;
            }
            if (in_array($menu_item['id'], $parent_id)) {
                continue;
            }
            unset($menu_list[$menu_key]);
        }
    }

    /**
     * 菜单类型转换
     * 根据菜单、目录、外链的不同类型转换为不同的菜单结构
     *
     * @param  array  $menu_list 数据库查询的菜单类型
     * @return void 返回转换后的数组
     */
    public static function menuType($menu_list)
    {
        $ret_list = [];
        $is_mobile = Check::checkMobileDevice(Request());
        foreach ($menu_list as $menu_item) {
            switch ($menu_item['type']) {
                case 'menu': // 菜单
                    $ret_list[] = [
                        'id' => $menu_item['id'],
                        'parent_id' => $menu_item['parent_id'],
                        'label' => $menu_item['label'],
                        'icon' => $menu_item['icon'],
                        'url' => $menu_item['url'],
                        'schemaApi' => $menu_item['path'],
                        'visible' => ($menu_item['visible'] == 0) || ($menu_item['visible'] == 1 && $is_mobile) || (! $menu_item['visible'] == 2),
                    ];
                    break;
                case 'dir': // 目录
                    $ret_list[] = [
                        'id' => $menu_item['id'],
                        'parent_id' => $menu_item['parent_id'],
                        'label' => $menu_item['label'],
                        'icon' => $menu_item['icon'],
                        'visible' => ($menu_item['visible'] == 0) || ($menu_item['visible'] == 1 && $is_mobile) || (! $menu_item['visible'] == 2),
                    ];
                    break;
                case 'link': // 外链
                    $ret_list[] = [
                        'id' => $menu_item['id'],
                        'parent_id' => $menu_item['parent_id'],
                        'label' => $menu_item['label'],
                        'icon' => $menu_item['icon'],
                        'link' => $menu_item['path'],
                        'visible' => ($menu_item['visible'] == 0) || ($menu_item['visible'] == 1 && $is_mobile) || (! $menu_item['visible'] == 2),
                    ];
                    break;
            }
        }

        return $ret_list;
    }
}
